home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / pdf / c++ / PageHistory < prev    next >
Text File  |  2003-02-14  |  5KB  |  185 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #include "PageHistory.h"
  39. #include "DocView.h"
  40. #include "Document.h"
  41. #include "PageHistory.h"
  42. #include "UserEvents.h"
  43.  
  44. PageHistory::PageHistory(DocView& v,DocViewToolbar& tb,GuiWindow& ancestor)
  45.  : view(v),
  46.    backIcon(tb,0x85),
  47.    forwardIcon(tb,0x86),
  48.    adjusterClickedTarget(&ancestor,GuiAdjuster::Clicked::Event,this,PageHistory::adjusterClicked),
  49.    historyBackTarget(&ancestor,User_HistoryBack,this,PageHistory::historyBack),
  50.    historyForwardTarget(&ancestor,User_HistoryForward,this,PageHistory::historyForward)
  51. {
  52.   begin=0;
  53.   end=0;
  54.   current=-1;
  55.   pendingPage=0;
  56. }
  57.  
  58. //****************************************************************
  59.  
  60. PageHistory::~PageHistory() {}
  61.  
  62. //****************************************************************
  63.  
  64. void PageHistory::add(int num)
  65. {
  66.   addPending();
  67.  
  68.   current++;
  69.   if (current==PAGE_HISTORY_SIZE) current=0;
  70.  
  71.   buf[current].page=num;
  72.   buf[current].docName=view.getDocument().getFileName();
  73.  
  74.   end=current+1;
  75.   if (end==PAGE_HISTORY_SIZE) end=0;
  76.  
  77.   if (end==begin)
  78.   {
  79.     begin=end+1;
  80.     if (begin==PAGE_HISTORY_SIZE) begin=0;
  81.   }
  82.  
  83.   setFade();
  84. }
  85.  
  86. //****************************************************************
  87.  
  88. const DocPage* PageHistory::back()
  89. {
  90.   addPending();
  91.  
  92.   if (current<0 || current==begin) return 0;
  93.   current--;
  94.   if (current<0) current+=PAGE_HISTORY_SIZE;
  95.   setFade();
  96.   return &buf[current];
  97.  
  98. }
  99.  
  100. //****************************************************************
  101.  
  102. const DocPage* PageHistory::forward()
  103. {
  104.   addPending();
  105.  
  106.   if (current<0) return 0;
  107.  
  108.   int next=current+1;
  109.   if (next==PAGE_HISTORY_SIZE) next=0;
  110.   if (next==end) return 0;
  111.   current=next;
  112.   setFade();
  113.   return &buf[current];
  114. }
  115.  
  116. //****************************************************************
  117.  
  118. void PageHistory::addPending(int num)
  119. {
  120.  pendingPage=num;
  121.  forwardIcon.setFlags(GuiToolboxGadget_Faded);
  122.  backIcon.setFlags(0);
  123. }
  124.  
  125. //****************************************************************
  126.  
  127. void PageHistory::addPending()
  128. {
  129.   if (pendingPage==0) return;
  130.   int page=pendingPage;
  131.   pendingPage=0;
  132.   add(page);
  133. }
  134.  
  135. //*************************************************************************
  136.  
  137. void PageHistory::setFade()
  138. {
  139.   backIcon.setFlags((current<0 || current==begin) ? GuiToolboxGadget_Faded : 0);
  140.   int next=current+1;
  141.   if (next==PAGE_HISTORY_SIZE) next=0;
  142.   forwardIcon.setFlags((current<0 || next==end) ? GuiToolboxGadget_Faded : 0);
  143. }
  144.  
  145. //*************************************************************************
  146.  
  147. void PageHistory::setPage(const DocPage* page)
  148. {
  149.   if (!page) return;
  150.   view.setPage(*page,0);
  151. }
  152.  
  153. //*************************************************************************
  154.  
  155. Claim PageHistory::historyBack(GuiToolboxEvent&,const GuiIdBlock&)
  156. {
  157.   setPage(back());
  158.   return CLAIM;
  159. }
  160.  
  161. //*************************************************************************
  162.  
  163. Claim PageHistory::historyForward(GuiToolboxEvent&,const GuiIdBlock&)
  164. {
  165.   setPage(forward());
  166.   return CLAIM;
  167. }
  168. //*************************************************************************
  169.  
  170. Claim PageHistory::adjusterClicked(GuiToolboxEvent& ev,const GuiIdBlock& idBlock)
  171. {
  172.   if (! (idBlock.self.component == backIcon.componentId() ||
  173.          idBlock.self.component == forwardIcon.componentId()) )return DONT_CLAIM;
  174.  
  175.   GuiAdjuster::Clicked& click = (GuiAdjuster::Clicked&)ev;
  176.  
  177.   if (click.isDown()) 
  178.      historyForward(ev,idBlock);
  179.   else
  180.      historyBack(ev,idBlock);
  181.      
  182.   return CLAIM;
  183. };
  184.  
  185.